package com.yuwei.service;

import com.yuwei.bean.Role;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.*;

@Service
public class RoleService {
    private int id = 123;
    private Map<Integer, Role> roleMap = new HashMap<>();

    public List<Role> getRoles(){
        Collection<Role> roles = roleMap.values();
        List<Role> rolesList = new ArrayList<>(roles);
        return rolesList;
    }

    private void addRole(Role role){
        roleMap.put(role.getId(),role);
    }

    private Role createRole(String roleName,String path){
        Role role = new Role(roleName, path);
        role.setId(id++);
        return role;
    }

    public List<Role> delRoleByID(int roleID, int mgrID, int typeID, int rightID){
        boolean b = roleMap.containsKey(roleID);
        if(!b){
            return null;
        }
        Role role = roleMap.get(roleID);
        Role mgr = role.getChildByID(mgrID);
        if(mgr == null){
            return null;
        }
        Role type = mgr.getChildByID(typeID);
        if(type == null){
            return null;
        }
        type.delRoleByID(rightID);
        return role.getChildren();
    }

    @PostConstruct
    private void init(){
        Role mgr = createRole("主管", "");
        mgr.setRoleDes("技术负责人");
        addRole(mgr);

        Role goodsMgr = createRole("商品管理", "goods");
        mgr.addRole(goodsMgr);

        Role goodsList = createRole("商品列表", "goods");
        Role clothes = createRole("衣服", "goods");
        goodsList.addRole(clothes);
        Role pants = createRole("裤子", "goods");
        goodsList.addRole(pants);
        goodsMgr.addRole(goodsList);

        Role params = createRole("分类参数", "goods");
        goodsMgr.addRole(params);

        Role goodsClass = createRole("商品分类", "goods");
        goodsMgr.addRole(goodsClass);

        Role orderMgr = createRole("订单管理", "orders");
        mgr.addRole(orderMgr);

    }
}
